#!/usr/bin/python

from sklearn import svm
from sklearn import preprocessing
import numpy
import sys
import pickle

execfile('/opt/python/includes/finance.py')

#selsvm=svm.SVC(kernel='poly',C=10000,verbose=True,degree=3)
#buysvm=svm.SVC(kernel='poly',C=10000,verbose=True,degree=3)
selsvm=svm.SVC(kernel='rbf',gamma=0.001,C=1000000,verbose=True)
buysvm=svm.SVC(kernel='rbf',gamma=0.001,C=1000000,verbose=True)

index='^FTSE'

inputs={}
X=[]
T=[]
ybuy=[]
tbuy=[]
ysel=[]
tsel=[]
datelist=(getOHLCDict(index))['date']

prices=getOHLCDict(index,'ohlcObjects')
for i in range(len(prices)-1,0,-1):
    if prices[i]['date'] in datelist: continue
    del prices[i]

for i in range(len(prices)-5):
    print "Comparing "+datefromsecs(prices[i]['date'])+" to " + datefromsecs(prices[i+1]['date'])+" and beyond...."

    prices[i]['close_delta1'] = prices[i]['close']-prices[i+1]['close']
    if prices[i]['close']<prices[i+1]['close']: prices[i]['close_up1'] = 0
    else: prices[i]['close_up1'] = 1;

    prices[i]['oc_delta1'] = prices[i]['close']-prices[i]['open']

    prices[i]['close_delta2'] = prices[i+1]['close']-prices[i+2]['close']
    if prices[i+1]['close']<prices[i+2]['close']: prices[i]['close_up2'] = 0
    else: prices[i]['close_up2'] = 1;

    prices[i]['oc_delta2'] = prices[i+1]['close']-prices[i+1]['open']

    prices[i]['close_delta3'] = prices[i+2]['close']-prices[i+3]['close']
    if prices[i+2]['close']<prices[i+3]['close']: prices[i]['close_up3'] = 0
    else: prices[i]['close_up3'] = 1;

    prices[i]['oc_delta3'] = prices[i+2]['close']-prices[i+2]['open']

    prices[i]['close_delta4'] = prices[i+3]['close']-prices[i+4]['close']
    if prices[i+3]['close']<prices[i+4]['close']: prices[i]['close_up4'] = 0
    else: prices[i]['close_up4'] = 1;

    prices[i]['oc_delta4'] = prices[i+3]['close']-prices[i+3]['open']

    inputs[prices[i]['date']]=[]
    inputs[prices[i]['date']].append(prices[i]['close_delta1'])
    inputs[prices[i]['date']].append(prices[i]['close_delta2'])
    inputs[prices[i]['date']].append(prices[i]['close_delta3'])
    inputs[prices[i]['date']].append(prices[i]['close_delta4'])
    inputs[prices[i]['date']].append(prices[i]['close_up1'])
    inputs[prices[i]['date']].append(prices[i]['close_up2'])
    inputs[prices[i]['date']].append(prices[i]['close_up3'])
    inputs[prices[i]['date']].append(prices[i]['close_up4'])
    inputs[prices[i]['date']].append(prices[i]['oc_delta1'])
    inputs[prices[i]['date']].append(prices[i]['oc_delta2'])
    inputs[prices[i]['date']].append(prices[i]['oc_delta3'])
    inputs[prices[i]['date']].append(prices[i]['oc_delta4'])

for i in range(len(prices)-1):
    if not prices[i]['date'] in inputs.keys(): continue
    today=prices[i]['date']
    tdi=i
    tomorrow=prices[i-1]['date']
    d1=i-1
    d2=i-2
    d3=i-3
    d4=i-4
    print str(i)+") Using inputs from "+datefromsecs(today)+" to calculate difference on "+datefromsecs(today)+" and "+datefromsecs(tomorrow)
    if(prices[d1]['close']>prices[tdi]['close'] and prices[d2]['close']>prices[tdi]['close'] and prices[d3]['close']>prices[tdi]['close']):
        if(i%10!=1):
            ybuy.append(1)
            ysel.append(0)
        else:
            tbuy.append(1)
            tsel.append(0)
    elif(prices[d1]['close']<prices[tdi]['close'] and prices[d2]['close']<prices[tdi]['close'] and prices[d3]['close']<prices[tdi]['close']):
        if(i%10!=1):
            ybuy.append(0)
            ysel.append(1)
        else:
            tbuy.append(0)
            tsel.append(1)
    else:
        if(i%10!=1):
            ybuy.append(0)
            ysel.append(0)
        else:
            tbuy.append(0)
            tsel.append(0)

    print len(inputs[today])
    if(i%10!=1): X.append(inputs[today])
    else: T.append(inputs[today])

T=numpy.array(T)
X=numpy.array(X)
#scaler=preprocessing.MinMaxScaler()
#X_norm = scaler.fit_transform(X)
#T_norm = scaler.fit_transform(T)
X_norm=preprocessing.scale(X)
T_norm=preprocessing.scale(T)
selsvm.fit(X_norm,numpy.array(ysel))
if len(sys.argv)>1: pickle.dump(selsvm,open(sys.argv[1]+"-sel","wb"))
buysvm.fit(X_norm,numpy.array(ybuy))
if len(sys.argv)>1: pickle.dump(buysvm,open(sys.argv[1]+"-buy","wb"))

correct=0
fail=0
for i in range(len(X_norm)):
    sellPrediction=selsvm.predict(X_norm[i])
    buyPrediction=buysvm.predict(X_norm[i])

    if(ybuy[i]==1 and sellPrediction==0 and buyPrediction==1): correct=correct+1
    if(ybuy[i]==1 and sellPrediction==1 and buyPrediction==0): fail=fail+1

    if(ysel[i]==1 and sellPrediction==1 and buyPrediction==0): correct=correct+1
    if(ysel[i]==1 and sellPrediction==0 and buyPrediction==1): fail=fail+1
    print "Buy: "+str(ybuy[i])+". Prediction: "+str(buyPrediction)+". Sell: "+str(ysel[i])+". Prediction: "+str(sellPrediction)+" "+str(fail)+" failed and "+str(correct)+" correct predictions"

print "In-Sample: After "+str(i+1)+" predictions, "+str(correct)+" were correct and "+str(fail)+" were incorrect"
  
correct=0
fail=0
for i in range(len(T_norm)):
    sellPrediction=selsvm.predict(T_norm[i])
    buyPrediction=buysvm.predict(T_norm[i])

    if(tbuy[i]==1 and sellPrediction==0 and buyPrediction==1): correct=correct+1
    if(tbuy[i]==1 and sellPrediction==1 and buyPrediction==0): fail=fail+1

    if(tsel[i]==1 and sellPrediction==1 and buyPrediction==0): correct=correct+1
    if(tsel[i]==1 and sellPrediction==0 and buyPrediction==1): fail=fail+1
    print "Buy: "+str(tbuy[i])+". Prediction: "+str(buyPrediction)+". Sell: "+str(tsel[i])+". Prediction: "+str(sellPrediction)+" "+str(fail)+" failed and "+str(correct)+" correct predictions"

print "Out-Sample: After "+str(i+1)+" predictions, "+str(correct)+" were correct and "+str(fail)+" were incorrect"
